/**
 * 网络请求配置
 */
import axios from 'axios';

axios.defaults.timeout = 100000;
// axios.defaults.baseURL = "http://test.mediastack.cn/";
// axios.defaults.baseURL = '/myapi';

switch (process.env.NODE_ENV) {
  case 'production':
    axios.defaults.baseURL = 'http://124.222.65.95:18581';
    break;
  case 'development':
    console.log(6666);
    axios.defaults.baseURL = '/jeecg-boot';
    break;
}

/**
 * http request 请求拦截器
 */
axios.interceptors.request.use(
  config => {
    config.data = JSON.stringify(config.data);
    config.headers.set('Content-Type', 'application/json');
    // config.headers.token = token// 设置token
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

/**
 * http response 响应拦截器
 */
axios.interceptors.response.use(
  response => {
    if (response.data.errCode === 2) {
      console.log('过期');
    }
    return response;
  },
  error => {
    console.log('请求出错：', error);
  }
);

/**
 * 封装get方法
 * @param url  请求url
 * @param params  请求参数
 * @returns {Promise}
 */
export function get(url: string, params = {}): Promise<any> {
  return new Promise((resolve, reject) => {
    axios
      .get(url, {
        params: params,
      })
      .then(response => {
        resolve(response.data);
      })
      .catch(error => {
        reject(error);
      });
  });
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function post(url: string, data: any): Promise<any> {
  return new Promise((resolve, reject) => {
    axios.post(url, data).then(
      response => {
        //关闭进度条
        resolve(response.data);
      },
      err => {
        reject(err);
      }
    );
  });
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function patch(url: string, data = {}): Promise<any> {
  return new Promise((resolve, reject) => {
    axios.patch(url, data).then(
      response => {
        resolve(response.data);
      },
      err => {
        msag(err);
        reject(err);
      }
    );
  });
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function put(url: string, data = {}): Promise<any> {
  return new Promise((resolve, reject) => {
    axios.put(url, data).then(
      response => {
        resolve(response.data);
      },
      err => {
        msag(err);
        reject(err);
      }
    );
  });
}

//统一接口处理，返回数据
export default function http(methods: any, url: any, param?: object | undefined) {
  return new Promise((resolve, reject) => {
    switch (methods) {
      case 'get':
        console.log('begin a get request,and url:', url);
        get(url, param)
          .then(function (response) {
            resolve(response);
          })
          .catch(function (error) {
            console.log('get request GET failed.', error);
            reject(error);
          });
        break;
      case 'post':
        post(url, param)
          .then(function (response) {
            resolve(response);
          })
          .catch(function (error) {
            console.log('get request POST failed.', error);
            reject(error);
          });
        break;
      default:
        break;
    }
  });
}

//失败提示
function msag(err: { response: { status: any; data: { error: { details: any } } } }) {
  if (err && err.response) {
    switch (err.response.status) {
      case 400:
        console.log(err.response.data.error.details);
        break;
      case 401:
        console.log('未授权，请登录');
        break;

      case 403:
        console.log('拒绝访问');
        break;

      case 404:
        console.log('请求地址出错');
        break;

      case 408:
        console.log('请求超时');
        break;

      case 500:
        console.log('服务器内部错误');
        break;

      case 501:
        console.log('服务未实现');
        break;

      case 502:
        console.log('网关错误');
        break;

      case 503:
        console.log('服务不可用');
        break;

      case 504:
        console.log('网关超时');
        break;

      case 505:
        console.log('HTTP版本不受支持');
        break;
      default:
    }
  }
}
